<?php
$nav_page = 1;
include_once 'common/personal_top.php';
include_once '../Class/DB.php';
include_once '../Class/User.php';
$userName=$_POST["name"];
$password=$_POST["password"];
$password2=$_POST["password2"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$address=$_POST["address"];
$sex=$_POST["sex"]=="male"?"0":"1";
$db=new DB();
$error_id=0;//用于标记错误位置
if($phone!=""&&strlen($phone)!=11)
{
	$error_id=6;
}
 if (ereg("/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/",$email)){
 	$error_id=5;
}
if($password!=$password2)
{
	$error_id=4;
}
if($userName!="")
{
if($userName!=$_SESSION["User"]["userName"]&&$db->Is_exist($userName))
{
	$error_id=2;
}
}

if (!isset($_POST["name"])){
	$error_id=-999;
}

if($error_id==0)
{
	$A_user=new User();	
	$A_user->userName=$_SESSION["User"]["userName"];
	$A_user->sex=$_SESSION["User"]["sex"];
	$A_user->email=$_SESSION["User"]["email"];
	$A_user->deliver_addr=$_SESSION["User"]["deliver_addr"];
	$A_user->phone=$_SESSION["User"]["phone"];
	$A_user->password=$_SESSION["User"]["password"];
	$A_user->level=$_SESSION["User"]["level"];
	$A_user->exp=$_SESSION["User"]["exp"];
	if($userName!="")
	{	
	$A_user->update_userName($userName);
	}
	if($sex!="")
	{
		$A_user->sex=$sex;	
	}
	if($email!="")
	{
		$A_user->email=$email;
	}
	if($password!=""&&$password2!="")
	{
	$A_user->update_Pwd($password);
	}
	if($phone!="")
	{
		$A_user->phone=$phone;
	}
	if($address!="")
	{
		$A_user->deliver_addr=$address;
	}
	$A_user->update_self();
	unset($_SESSION["User"]);
	$_SESSION["User"]=$db->get_userName_info($A_user->userName);
	//exit();
    ?>
<script language="JavaScript">
window.location.href="personal.php";
</script>
<?php	
}
?>
<div id="info_change" class="block_mode">
		<div class="block_context">
			<form action="personal_change.php" method="post">
				<table>
					<tr>
						<th class="t1">
						</th>
						<th class="t2">
						</th>
						<th class="t3">
						</th>
					</tr>
					<tr>
						<td>
						<b>用户名:</b>
						</td>
						<td><input type="text" name="name" value="" autocomplete="off" /></td>
						<?php if($error_id==2){?>
						<td>
							<p>用户名已存在!</p>
						</td>
						<?php 	}?>
						
					</tr>
					<tr>
						<td>
						<b>密码:</b>
						</td>
						<td><input type="password" name="password" value=""/></td>
					</tr>
					<tr>
						<td>
						<b>确认密码:</b>
						</td>
						<td><input type="password" name="password2" value=""/></td>
						<?php if($error_id==4){?>
						<td>
							<p>请再次确认密码!</p>
						</td>
						<?php 	}?>
					</tr>
					<tr>
						<td>
						<b>性别:</b>
						</td>
						<td>
							<input type="radio" name="sex" value="male" checked="checked"/>
							<label>男</label>
							<input type="radio" name="sex" value="female" />
							<label>女</label>
						</td>
					</tr>
					<tr>
						<td>
						<b>Email:</b>
						</td>
						<td><input type="text" name="email" value=""/></td>
							<?php if($error_id==5){?>
						<td>
							<p>请输入正确的邮箱地址!</p>
						</td>
						<?php 	}?>
					</tr>
					<tr>
						<td>
						<b>手机:</b>
						</td>
						<td><input type="text" name="phone" value=""/></td>
							<?php if($error_id==6){?>
						<td>
							<p>请输入正确的手机号!</p>
						</td>
						<?php 	}?>
					</tr>
					<tr>
						<td>
						<b>常用收货地址:</b>
						</td>
						<td><input type="text" name="address" value=""/></td>
					</tr>
					
					<tr>
					<tr>
						<td></td>
						<td><input type="submit" value="" /></td>
						<td></td>
					</tr>
				</table>
		</form>
	</div>
</div>

<?php
include_once 'common/personal_tail.php';
?>